Added initial documentation tree using doxygen. More tweaks on the license text ensur...
[lwes-dotnet/github-mirror.git] / Test Projects / Lwes-test-listener-console / Program.cs
blob05202841a68ec589b2586f6699204b71ebfc850d
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.Tests
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
26 using System.Threading;
28 using Org.Lwes;
29 using Org.Lwes.Listener;
31 class Program
33 #region Methods
35 static void Main(string[] args)
37 using (IEventListener listener = EventListener.CreateDefault())
39 EventSink sink = new EventSink();
40 listener.RegisterEventSink(sink).Activate();
41 ManualResetEvent printerExited = new ManualResetEvent(false);
42 bool userChoseToExit = false;
43 int receivedEvents = 0;
45 Console.WriteLine("LWES EventListener -\r\n This console will continue to queue and print LWES events until\r\n the user types 'exit' followed by a carriage return.");
47 Object consoleWriteLock = new Object();
49 ThreadPool.QueueUserWorkItem(new WaitCallback((s) =>
51 Event ev;
52 while (!userChoseToExit)
54 if (sink.Events.TryDequeue(out ev))
56 lock (consoleWriteLock)
58 Console.WriteLine(ev.ToString(true));
60 receivedEvents++;
62 else
64 Thread.Sleep(200);
67 printerExited.Set();
68 }));
70 ThreadPool.QueueUserWorkItem(new WaitCallback((s) =>
72 while (!userChoseToExit)
74 int eventCount = receivedEvents;
75 Thread.Sleep(2000);
76 if (eventCount < receivedEvents)
78 lock (consoleWriteLock)
80 Console.WriteLine("LWES EventListener -\r\n This console will continue to queue and print LWES events until\r\n the user types 'exit' followed by a carriage return.");
84 }));
86 string input;
89 input = Console.ReadLine();
90 } while (!String.Equals(input, "exit", StringComparison.CurrentCultureIgnoreCase));
91 userChoseToExit = true;
92 listener.Dispose();
94 printerExited.WaitOne();
98 #endregion Methods
100 #region Nested Types
102 class EventSink : IEventSink
104 #region Fields
106 SimpleLockFreeQueue<Event> _incomingEvents = new SimpleLockFreeQueue<Event>();
108 #endregion Fields
110 #region Properties
112 public SimpleLockFreeQueue<Event> Events
114 get { return _incomingEvents; }
117 public bool IsThreadSafe
119 get { return false; }
122 #endregion Properties
124 #region Methods
126 public void HandleEventArrival(IEventSinkRegistrationKey key, Event ev)
128 _incomingEvents.Enqueue(ev);
131 public GarbageHandlingVote HandleGarbageData(IEventSinkRegistrationKey key, System.Net.EndPoint remoteEndPoint, int priorGarbageCountForEndpoint, byte[] garbage)
133 return GarbageHandlingVote.IgnoreAllTrafficFromEndpoint;
136 #endregion Methods
139 #endregion Nested Types